package com.inspire.tank.cor;

import com.inspire.tank.GameObject;
import com.inspire.tank.PropertyMgr;

import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;

/**
 * @author inspire
 * @description
 * @date 2022/6/16 20:58
 */
public class ColliderChain implements Collider{
    private LinkedList<Collider> colliders=new LinkedList<>();
    public ColliderChain(){
        String colliders = PropertyMgr.get("colliders").toString();
        String[] colliders_arr = colliders.split(";");
        for(String collider:colliders_arr){
            System.out.println("collider:"+collider);
            try {
                add((Collider) Class.forName(collider).getDeclaredConstructor().newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public void add(Collider collider){
        colliders.add(collider);
    }

    public boolean collide(GameObject gameObject1, GameObject gameObject2) {
        for(int i=0;i<colliders.size();i++){
            if(!colliders.get(i).collide(gameObject1,gameObject2)){
                return false;
            }
        }
        return false;
    }
}
